package com.pkx.guide;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Description 代码生成器
 * @Author sunsy
 * @date 2022.06.08 13:18
 */

public class CodeGenerator {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        // InputStream in = CodeGenerator.class.getClassLoader().getResourceAsStream("application.properties");
        InputStream in = CodeGenerator.class.getClassLoader().getResourceAsStream("application-pkx.properties");
        // 使用properties对象加载输入流
        properties.load(in);
        // 创建代码生成器
        FastAutoGenerator.create(properties.getProperty("pkx.dataSourceUrl"), properties.getProperty("pkx.username"), properties.getProperty("pkx.password"))
                .globalConfig(builder -> {
                    builder.author(properties.getProperty("pkx.author"))
                            .enableSwagger()
                            .fileOverride()
                            .dateType(DateType.ONLY_DATE)
                            .outputDir(properties.getProperty("pkx.projectPath") + "/src/main/java");
                })
                .packageConfig(builder -> {
                    builder.parent(properties.getProperty("pkx.package"))
                            .controller("controller")
                            .mapper("mapper")
                            .service("service")
                            .entity("entity")
                            .moduleName(properties.getProperty("pkx.moduleName"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(properties.getProperty("pkx.tableName").split(","))
                            //entity配置
                            .entityBuilder()
                            .enableLombok()
//                            .superClass(com.pkx.guide.common.base.BaseEntity.class)//继承父类
                            .logicDeleteColumnName("del_flag")//逻辑删除字段
                            .enableTableFieldAnnotation()
                            .idType(IdType.ASSIGN_ID)
                            .naming(NamingStrategy.underline_to_camel)
                            .addSuperEntityColumns(properties.getProperty("pkx.superEntityColumns").split(","))
                            //controller配置
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .enableHyphenStyle()
                            //service配置
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .superServiceClass(IService.class)
                            //dao配置
                            .mapperBuilder()
                            .formatXmlFileName("%sMapper")
                            .enableMapperAnnotation();
                })
                .templateConfig((scanner, builder) -> builder
                                .disable()//禁用所有模板
                                //.entityKt("/templates/entity.java")// 设置实体模板路径(kotlin)，/templates/entity.java
//						.disable(TemplateType.ENTITY)//禁用模板 TemplateType.ENTITY
                                .entity("templates/entity.java") //设置实体模板路径(JAVA)，/templates/entity.java
                                .service("templates/service.java")//设置 service 模板路径，/templates/service.java
                                .serviceImpl("templates/serviceImpl.java")//设置 serviceImpl 模板路径，/templates/serviceImpl.java
                                .mapper("templates/mapper.java")//设置 mapper 模板路径，/templates/mapper.java
                                .xml("templates/mapper.xml")//设置 mapperXml 模板路径，/templates/mapper.xml
                                .controller("templates/controller.java")//设置 controller 模板路径，/templates/controller.java
                                .build()//加入构建队列
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}




